#ifndef LIBUV_SERVLET_H
#define LIBUV_SERVLET_H
#include "IServlet.h"
#include <vector>
#include "uv.h"
class LibuvClientServlet : public IServlet {
public:
    virtual void on(SERVLET_STATUS type,std::function<int(ISession *session,void*,int)> handler);
    virtual void request(const void *data,int len);
    virtual void start();
    virtual void wait();
    virtual void stop();
    virtual void onLoad(INode *node);
    virtual void onUnload();
    virtual void onMessage(ISession *session,void* data,int size);
    virtual void onState(SERVLET_STATUS status,ISession *session,int code);
    virtual ~LibuvClientServlet();
private:
    std::string ip;
    int port = 0;
    std::map<int,std::vector<std::function<int(ISession *,void*,int)> > >  msgidHandlers;
public:   
    uv_loop_t* loop = NULL;
    bool withLoop = false;
    uv_tcp_t tcp_client;
    uv_stream_t *stream = NULL;
    uv_connect_t connect_req;
    ISession *clientSession = NULL;
};

class LibuvServerServlet : public IServlet {
public:
    virtual void on(SERVLET_STATUS type,std::function<int(ISession *session,void*,int)> handler);
    virtual void request(const void *data,int len);
    virtual void start();
    virtual void wait();
    virtual void stop();
    virtual void onLoad(INode *node);
    virtual void onUnload();
    virtual void onMessage(ISession *session,void* data,int size);
    virtual void onState(SERVLET_STATUS status,ISession *session,int code);
    virtual ~LibuvServerServlet();
private:
    std::string ip;
    int port = 0;
    bool withLoop = true;
    std::map<int,std::vector<std::function<int(ISession *,void*,int)> > >  msgidHandlers;
public:   
    uv_loop_t* loop;
    uv_tcp_t tcpServer;
};

#endif // LIBUV_SERVLET_H